//
//  LillyGoldLsitController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/7/11.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "LillyGoldLsitController.h"
#import "AccountChangeModel.h"
#import "AccountChangeCell.h"

@interface LillyGoldLsitController ()
<UITableViewDelegate,UITableViewDataSource>
{
    NSUInteger _refreshIndex;
    BOOL _judgeList;
    UIImageView *noDrawImage;
}

@property(nonatomic,strong)UITableView *lillyView;
@property(nonatomic,strong)NSMutableArray *lillyArray;
@property(nonatomic,strong)MBProgressHUD *progressHUD;

@end

@implementation LillyGoldLsitController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    self.title = @"让利金转换记录";
    [self.view addSubview:self.lillyView];
    self.view.backgroundColor = BACKGROUND_COLOR;
    
    _refreshIndex = 1;
    
    [self loadLillyGoldListData];
    [self addPullUpdateData];
}

#pragma mark >>>>>>>>刷新
- (void)addPullUpdateData
{
    self.lillyView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreLillyListData)];
    
}

- (void)loadMoreLillyListData
{
    _refreshIndex ++;
    [self loadLillyGoldListData];
    [self.lillyView.mj_footer endRefreshing];
}



- (void)loadLillyGoldListData
{
    _progressHUD = [MBProgressHUD show:self.view andMessage:nil];
    [self.view addSubview:_progressHUD];
    
    
    NSString *page = [NSString stringWithFormat:@"%ld",_refreshIndex];
    
    NSDictionary *params = @{
                             @"username":USERNAME,
                             @"amount_type":@"3",
                             @"page":page
                             };
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/bankroll/bankrollLog" completeBlock:^(id object) {
       
        if (JudgeBackDataCode) {
            
            NSArray *arr = [object objectForKey:@"data"];
            for (NSDictionary *dict in arr) {
                AccountChangeModel *accountModel = [[AccountChangeModel alloc] initWithDictionary:dict error:nil];
                if (accountModel) {
                    [self.lillyArray addObject:accountModel];
                }
                
            }
            
        }else{
            
            if (_refreshIndex == 1) {
                
                noDrawImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_NoRecord"]];
                [self.view addSubview:noDrawImage];
                
                [noDrawImage mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(self.view.mas_centerX);
                    make.centerY.equalTo(self.view.mas_centerY).offset(-HEIGHT/8);
                    make.width.mas_equalTo(WIDTH/1.5);
                    make.height.mas_equalTo(WIDTH/2.4);
                }];
                
            }else{
                
                [self.lillyView.mj_footer endRefreshingWithNoMoreData];
            }
    
        }
        [_progressHUD hide:YES];
        [self.lillyView reloadData];
        
        
    }];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.lillyArray.count ;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AccountChangeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"lillyView" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    AccountChangeModel *accountModel = self.lillyArray[indexPath.section];
    cell.userLbl.text = accountModel.username;
    cell.descLbl.text = accountModel.details;
    cell.numLbl.text = accountModel.funds_chang_amunt;
    cell.timeLbl.text = accountModel.add_time;
    
    return cell;
}



- (UITableView *)lillyView
{
    if (!_lillyView) {
        _lillyView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,WIDTH,HEIGHT-64) style:UITableViewStylePlain];
        _lillyView.backgroundColor = BACKGROUND_COLOR;
        _lillyView.delegate =self;
        _lillyView.dataSource =self;
        _lillyView.rowHeight = 55;
        _lillyView.tableFooterView = [[UIView alloc] init];
        
        [_lillyView registerClass:[AccountChangeCell class] forCellReuseIdentifier:@"lillyView"];
    }
    return _lillyView;
}

- (NSMutableArray *)lillyArray
{
    if (!_lillyArray) {
        _lillyArray = [NSMutableArray array];
    }return _lillyArray;
}


@end
